package org.gruposp2p.aula.gwt.client.content.coursegroup;

import java.util.ArrayList;
import java.util.List;
import org.gruposp2p.aula.gwt.client.AulaApplication;
import org.gruposp2p.aula.gwt.client.AulaConstants;
import org.gruposp2p.aula.gwt.client.AulaContentWidget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class CoursegroupPanelWidget extends AulaContentWidget {
	
	public static interface CwConstants extends Constants {	
		String CoursegroupPanelWidgetTabTitle();
		String DataButtonText();
		String SubjectsButtonText();
		String ItemsButtonText();
		String ScoresButtonText();
	}	
	
	private AulaApplication aulaApplication;
	private AulaConstants constants;
	private ToggleButton dataToggleButton;
	private ToggleButton subjectsToggleButton;
	private ToggleButton itemsEvaluablesToggleButton;
	private ToggleButton scoresToggleButton;
	private List<ToggleButton> toggleButtons;
	private CoursegroupDataPanel dataPanel;
	private CoursegroupItemsEvaluablesPanel itemsPanel;
	private CoursegroupScoresPanel scoresPanel;
	private CoursegroupSubjectsPanel subjectsPanel;
	private Panel selectePanel;
	private VerticalPanel tabbedPanel;	

	public CoursegroupPanelWidget (AulaApplication aulaApplication) {
		this.aulaApplication = aulaApplication;
		this.constants = aulaApplication.getConstants();
		toggleButtons = new ArrayList<ToggleButton>();
		dataToggleButton = new ToggleButton(constants.DataButtonText());
		dataToggleButton.addClickHandler(dataButtonHandler);
		dataToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(dataToggleButton);
		subjectsToggleButton = new ToggleButton(constants.SubjectsButtonText());
		subjectsToggleButton.addClickHandler(subjectsButtonHandler);
		subjectsToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(subjectsToggleButton);
		itemsEvaluablesToggleButton = new ToggleButton(constants.ItemsButtonText());
		itemsEvaluablesToggleButton.addClickHandler(itemsButtonHandler);
		itemsEvaluablesToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(itemsEvaluablesToggleButton);
		scoresToggleButton = new ToggleButton(constants.ScoresButtonText());
		scoresToggleButton.addClickHandler(scoresButtonHandler);
		scoresToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(scoresToggleButton);		
		dataPanel = new CoursegroupDataPanel(aulaApplication);
		itemsPanel = new CoursegroupItemsEvaluablesPanel(aulaApplication);
		scoresPanel = new CoursegroupScoresPanel (aulaApplication);
		subjectsPanel = new CoursegroupSubjectsPanel(aulaApplication);		
	    tabbedPanel = new VerticalPanel();
	}

	private void setSelectedPanel(Panel panel) {	
		tabbedPanel.remove(selectePanel);
		this.selectePanel = panel;
		tabbedPanel.add(panel);		
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return constants.CoursegroupPanelWidgetTabTitle();
	}

	@Override
	public Widget onInitialize() {
		HorizontalPanel toggleButtonPanel = new HorizontalPanel();
		toggleButtonPanel.setStyleName("AulaWidgetInternalTab");
		toggleButtonPanel.add(dataToggleButton);
		toggleButtonPanel.add(subjectsToggleButton);
		toggleButtonPanel.add(itemsEvaluablesToggleButton);
		toggleButtonPanel.add(scoresToggleButton);
		tabbedPanel.add(toggleButtonPanel);		
		dataToggleButton.setDown(true);
		selectePanel = dataPanel;
		tabbedPanel.add(selectePanel);
	    return tabbedPanel;
	}
	
	private void setButtonsState (ClickEvent event) {
		ToggleButton source = (ToggleButton) event.getSource();
		for (ToggleButton toggleButton : toggleButtons) {
			if (!toggleButton.equals(source)) {
				toggleButton.setDown(false);
			} else {
				toggleButton.setDown(true);
			}
		}
	}
	
	ClickHandler dataButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);
			setSelectedPanel(dataPanel);
			
		}		
	};

	ClickHandler subjectsButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);
			setSelectedPanel(subjectsPanel);
		}		
	};	
	
	ClickHandler itemsButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);
			setSelectedPanel(itemsPanel);
		}		
	};
	
	ClickHandler scoresButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);
			setSelectedPanel(scoresPanel);
		}		
	};	
	
}